﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework; // Vector2 e demais classes para posicionamento.
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace first_project
{
    class Shot
    {
        private GameWindow window;
        private Texture2D image;
        private Vector2 position;
        private Vector2 direction;
        private Vector2 speed = new Vector2(5, 5);
        private Vector2 lifespan;
        public bool isActive
        {
            get
            {
                if (this.lifespan.X <= 0 || this.lifespan.Y <= 0)
                {
                    return false;
                }
                return true;
            }
        }
        private float damage;
        public float Damage
        {
            get
            {
                return damage;
            }
            set
            {
                if (value >= 5)
                    damage = 5;
                else if (value <= 1)
                    damage = 1;
                else
                    damage = value;
            }
        }

        /// <summary>
        /// Getter for asteroid collision rectangle.
        /// </summary>
        public Rectangle CollisionRectangle
        {
            get
            {
                return new Rectangle((int)position.X, (int)position.Y, image.Width, image.Height);
            }
        }

        public Shot(GameWindow window, Texture2D image, Vector2 position, Vector2 direction)
        {
            this.window = window;
            this.image = image;
            this.position = position;
            this.direction = direction;
            this.lifespan = new Vector2(window.ClientBounds.Width * 1.5f, window.ClientBounds.Height * 1.5f);
        }

        public void Update(GameTime gameTime)
        {
            moveShot();
            processLifespan();
        }


        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(image, position, null, Color.White, 0, new Vector2(image.Width / 2, image.Height / 2), 1, SpriteEffects.None, 0);

        }

        // Método responsável por verificar o "tempo de vida" do tiro.
        private void processLifespan()
        {
            float travelX = (direction * speed).X;
            float travelY = (direction * speed).Y;

            if (travelX < 0 && travelY < 0)
                this.lifespan += direction * speed;
            else if (travelX < 0)
            {
                this.lifespan.X += travelX;
                this.lifespan.Y -= travelY;
            }
            else if (travelY < 0)
            {
                this.lifespan.X -= travelX;
                this.lifespan.Y += (direction * speed).Y;
            }
            else
                this.lifespan -= direction * speed;
        }

        // Método responsável pelo moviento dos tiros.
        private void moveShot()
        {
            position += direction * speed;

            if (position.X >= window.ClientBounds.Width + image.Width / 2)
            {
                position.X = -image.Width / 2;
            }
            else if (position.X <= -image.Width / 2)
            {
                position.X = window.ClientBounds.Width + image.Width / 2;
            }
            else if (position.Y >= window.ClientBounds.Height + image.Height / 2)
            {
                position.Y = -image.Height / 2;
            }
            else if (position.Y <= -image.Height / 2)
            {
                position.Y = window.ClientBounds.Height + image.Height / 2;
            }
        }
    }
}
